/*
 * Copyright (C) 2023 Huawei Device Co., Ltd.
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

// import { JSONData } from './lib/model.js';
import { BehaviorTree } from './lib/BehaviorTree.js';
import { BehaviorTreeImporter } from './lib/BehaviorTreeImporter.js';
import { Random } from './lib/Random.js';
import { Task } from './lib/Task.js';
import { constants } from './lib/constants.js';
import { Sequence } from './lib/Sequence.js'
import { Selector } from './lib/Selector.js';
import { Parallel } from './lib/Parallel.js'
import { ParallelComplete } from './lib/ParallelComplete.js'
const forNumber2 = 1000;
const forNumber3 = 100;

class Index {
  timerId = 0;
  flag = true;

  behaviorTreeImport = new BehaviorTreeImporter();
  mySelector = new Random({
    nodes: []
  })
  bTree = new BehaviorTree({ tree: this.mySelector, blackboard: {} });
  constructor() {
    this.init();
    const aTask = new Task({
      run() {
        // print("小明选择了计算机专业");
        return undefined;
      },
    })
    const bTask = new Task({
      run() {
        // print("小明选择了教师专业");
        return undefined;
      },
    })
    const switchTask = new Task({
      run() {
        const value = Math.random() * 10;
        if (value >= 5) {
          // print("通过了--华为--面试");
          return constants.SUCCESS;
        } else {
          // print("未通过--华为--面试");
          return constants.FAILURE;
        }
      },
    })
    this.mySelector = new Sequence({
      nodes: [
        aTask,
        bTask,
        switchTask,
      ]
    })
    this.bTree = new BehaviorTree({ tree: this.mySelector, blackboard: {} })
  }

  init() {

  }
}

let index = new Index()
function step_test() {
  let startTime = Date.now();
  //防止多次设置重复调用行为树启动
  if (index.flag) {
    index.flag = false;
    for (let i = 0; i < forNumber2; i++) {
      index.bTree.step();
    }
  }
  let endTime = Date.now();
  // print(`1111-${index.bTree.tree.nodes[1].nodeType}`)
  print(`behaviorTree_step: ${endTime - startTime} ms`);
}

function run_test_01() {
  const switchTask = new Task({
    start: (blackboard) => {
      ++blackboard.start;
    },
    run: (blackboard) => {
      ++blackboard.run;
      return blackboard.switchResult;
    },
    end: (blackboard) => {
      ++blackboard.end;
    }
  });
  const runningTask = new Task({
    run: () => {
      return constants.RUNNING;
    }
  });
  const parallel = new Parallel({
    nodes: [runningTask, switchTask, runningTask]
  });
  const blackboard = {
    switchResult: constants.RUNNING,
    start: 0,
    run: 0,
    end: 0
  };
  let result;
  let startTime = Date.now();
  for (let i = 0; i < 10000; i++) {
     result = parallel.run(blackboard);
  }
  let endTime = Date.now();

  if (blackboard.start != 10000) throw Error(`run element != 10000`);
  print(`behaviorTree_run(switchTask): ${endTime - startTime} ms`);

  // print(`${blackboard.start}`);
  // print(`${blackboard.run}`);
  // print(`${blackboard.end}`);
  // print(`${String(result.total)}`);
}

function run_test_02() {
  let countSuccess = 0;
  const successTask = new Task({
    run: () => {
      ++countSuccess;
      return constants.SUCCESS;
    }
  });
  let countRunning = 0;
  const runningTask = new Task({
    run: () => {
      ++countRunning;
      return constants.RUNNING;
    }
  });
  const parallelComplete = new ParallelComplete({
    nodes: [runningTask, successTask, runningTask]
  });

  let result;
  let startTime = Date.now();
  for (let i = 0; i < 10000; i++) {
     result = parallelComplete.run();
  }
  let endTime = Date.now();
  if (countSuccess != 10000) throw Error(`run element != 10000`);
  print(`behaviorTree_run(successTask): ${endTime - startTime} ms`);

  // print(`${countSuccess}`);
  // print(`${countRunning}`);
  // print(`${String(result)}`);
}

let loopCountForPreheat = 1;
for (let i = 0; i < loopCountForPreheat; i++) {
  index = new Index()
  step_test()
  run_test_01()
  run_test_02()
}

ArkTools.waitAllJitCompileFinish();

index = new Index()
step_test()
run_test_01()
run_test_02()